<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-11
 * Time: 09:18
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\FacilitySort;
use app\api\model\HotelRoom;
use app\api\model\HotelRoomOrder;
use app\api\service\hotel\HotelRoomOrderSer;
use app\api\service\hotel\JudgeRoom;
use app\api\validate\IdMustBePositiveInt;
use app\api\validate\Paging;
use app\api\model\City;
use app\api\model\System;
use think\facade\Request;
use app\api\service\Token;

class Hotel extends BaseController
{
    protected $beforeActionList = [
        'checkPrimaryScope' => [ 'only' => 'getNewHotelRoom,getRecommendHotelRoom,getHotelRoom,getHotelRoomInfo' ]
    ];

    // 获取上新房间
    public function getNewHotelRoom($city)
    {
        $info = City::where('name','like','%'.$city.'%')->find();
        if($info) {
            return HotelRoom::where('city_id',$info['id'])->with('imgs.img,admin')->order('id desc')->limit(5)->select()->toArray();
        } else {
            return HotelRoom::with('imgs.img,admin')->order('id desc')->limit(5)->select()->toArray();
        }
    }

    // 获取推荐
    public function getRecommendHotelRoom($page,$limit,$city)
    {
        (new Paging())->goCheck();
        $info = City::where('name','like','%'.$city.'%')->find();
        if($info) {
            $query = HotelRoom::with('imgs.img,admin,roomTag')
                ->where('recommend',2)
                ->where('city_id',$info['id'])
                ->order('order_sort desc');
        } else {
            $query = HotelRoom::with('imgs.img,admin,roomTag')
                ->where('recommend',2)
                ->order('order_sort desc');
        }
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    // 根据城市获取房间列表
    public function getHotelRoom($page,$limit,$city_id)
    {
        (new Paging())->goCheck();
        $query = HotelRoom::with('imgs.img,admin,roomTag')
            ->where('city_id',$city_id)
            ->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    // 根据ID获取详情
    public function getHotelRoomInfo($id)
    {
        (new IdMustBePositiveInt())->goCheck();
        $system = System::get(1);
        $info = HotelRoom::where('id',$id)
            ->with([
                'imgs' => [
                    'img'
                ],
                'admin' => [
                    'adminTraffic' => [
                        'traffic' => [ 'img' ]
                    ]
                ],
                'roomBasis' => [
                    'basis' => [ 'img' ]
                ],
                'roomTag',
                'roomClaim',
                'roomFacility' => [
                    'facility' => [ 'img' ]
                ],
            ])->find();
        if($info['room_facility']) {
            $facilitySort = FacilitySort::order('order_sort desc')->select()->toArray();
            foreach ($facilitySort as $k=>$v) {
                $facilitySort[$k]['facility'] = [];
                foreach ($info['room_facility'] as $ko=>$vo) {
                    if($facilitySort[$k]['id'] == $info['room_facility'][$ko]['facility']['sort_id']) {
                        array_push($facilitySort[$k]['facility'],$info['room_facility'][$ko]);
                    }
                }
                if(count($facilitySort[$k]['facility']) == 0) {
                    unset($facilitySort[$k]);
                }
            }
            sort($facilitySort);
        }
        unset($info['room_facility']);
        $info['facility_sort'] = $facilitySort;
        $info['booking_instructions'] = $system['booking_instructions'];
        return $info;
    }

    // 判断预订情况
    public function judgeRoom($hotel_room_id,$start_date,$end_date)
    {
        $hotel = new JudgeRoom($hotel_room_id,$start_date,$end_date);
        return $hotel->entrance();
    }

    /**
     *  订房下单
     * return 支付number
     */
    public function createOrder()
    {
        $post = Request::post();
        $hotelRoomOrder = new HotelRoomOrderSer($post);
        return $hotelRoomOrder->entrance();
    }

    /**
     * 订房订单
     */
    public function getOrderList()
    {
        $uid = Token::getCurrentUid();
        return HotelRoomOrder::where('user_id','=',$uid)
            ->where('status','gt',1)
            ->with('img')
            ->order('id desc')
            ->hidden(['out_trade_no','prepay_id','pay_overdue_time', 'create_time','update_time','delete_time'])
            ->select()->each(function ($item){
                if($item['refund_time']) {
                    $item['refund_time'] = date('Y/m/d H:i:s',$item['refund_time']);
                }
                $item['pay_time'] = date('Y/m/d H:i:s',$item['pay_time']);
            })->toArray();
    }

    /**
     * 订房订单详情
     */
    public function getOrderInfo($id)
    {
        $uid = Token::getCurrentUid();
        $info = HotelRoomOrder::where('user_id',$uid)
            ->where('id',$id)
            ->with('img')
            ->hidden(['out_trade_no','prepay_id','pay_overdue_time', 'create_time','update_time','delete_time'])
            ->find();
        if($info['refund_time']) {
            $info['refund_time'] = date('Y/m/d H:i:s',$info['refund_time']);
        }
        $info['pay_time'] = date('Y/m/d H:i:s',$info['pay_time']);
        return $info;
    }
}